//
//  FunctionViewController.m
//  MarryMe
//
//  Created by lanou3g on 16/1/5.
//  Copyright © 2016年 revocher. All rights reserved.
//

#import "FunctionViewController.h"
#import "FunScendViewController.h"
#import "CalendarViewController.h"
#import "BudgetViewController.h"
#import "AccountViewController.h"
#import "UIScrollView+BalloonPullToRefresh.h"
#define KIE [UIScreen mainScreen].bounds.size.width
@interface FunctionViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (strong, nonatomic) IBOutlet UITableView *tableView;
@property (strong,nonatomic)NSMutableArray *array;
@property (strong,nonatomic)NSArray *arr;
@property (strong,nonatomic)UIButton *button;
@property (strong,nonatomic)NSString *string;

//刷新
@property (nonatomic, strong) NSTimer *workTimer;
@property (nonatomic, strong) NSMutableArray *items;
@end

@implementation FunctionViewController
@synthesize workTimer = _workTimer;
@synthesize items = _items;

- (NSMutableArray *)items {
    if (_items == nil) {
        _items = [NSMutableArray new];
    }
    return _items;
}

- (void)statTodoSomething {
    
    [self.workTimer invalidate];
    
    self.workTimer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(onAllworkDoneTimer) userInfo:nil repeats:NO];
}

- (void)onAllworkDoneTimer {
    [self.workTimer invalidate];
    self.workTimer = nil;
    
    [self.items addObject: [NSNumber numberWithInt: self.items.count]];
    
    [self.tableView.pullToRefreshController didFinishRefresh];
    [self.tableView reloadData];
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self setHidesBottomBarWhenPushed:NO];
     [self.navigationController setNavigationBarHidden:YES animated:YES];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title=@"结婚助手";
    [self initTestData];
    __typeof (&*self) __weak weakSelf = self;
    
    [self.tableView addPullToRefreshWithActionHandler:^ {
        if (!weakSelf.tableView) {
            [weakSelf.tableView setFrame:CGRectMake(0, 64, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height-64)];
        }
        
        int64_t delayInSeconds = 1.0;
        dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
        dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
            [weakSelf refreshTriggered];
        });
    }];
    self.tableView.pullToRefreshController.waitingAnimation = BalloonPullToRefreshWaitAnimationFade;
//    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(passtVule:) name:@"hh" object:nil];
}
- (void)refreshTriggered {
    [self statTodoSomething];
}


-(void)passtVule:(NSNotification*)sender{
    self.string=sender.userInfo[@"key1"];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
  
    return self.arr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
  NSString *cell_id =@"cell_id";
    UITableViewCell *cell =[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cell_id];
    if (!cell) {
        cell =[tableView dequeueReusableCellWithIdentifier:cell_id];
    }
    NSString *str =  self.arr[indexPath.row];
    NSArray *data=self.array[indexPath.row];
    UILabel *label=[[UILabel alloc]initWithFrame:CGRectMake((KIE-100)/2, 25, 100, 40)];
    label.textAlignment= NSTextAlignmentCenter;
    label.alpha=0.3;
    label.text=str;
    label.textColor=[UIColor greenColor];
    label.backgroundColor=[UIColor redColor];
    label.layer.cornerRadius=20;
    label.clipsToBounds=YES;
    [cell.contentView addSubview:label];
    NSLog(@"%lu",(unsigned long)data.count);
    for (int i=0;i<data.count; i++) {
      self.button=[UIButton buttonWithType:UIButtonTypeCustom];
        self.button.frame=CGRectMake((KIE/3)*(i%3),200*(i/3)+(KIE/3)-50, KIE/3,  KIE/3);
      self.button.tag = indexPath.row*(i+1)*10+100+i;
        NSLog(@"%ld",(long)self.button.tag);
       //button.backgroundColor=[UIColor grayColor];
        [self.button addTarget:self action:@selector(picButtonClickAction:) forControlEvents:UIControlEventTouchUpInside];
        UIImageView *imagView=[[UIImageView alloc]initWithFrame:CGRectMake(30, 10, (KIE/3)-60, (KIE/3)-65)];
        imagView.image=[UIImage imageNamed:[data[i]valueForKey:@"image"]];
        UILabel *labeltext=[[UILabel alloc]initWithFrame:CGRectMake(20,(KIE/3)-45, (KIE/3)-40, 35)];
        labeltext.textAlignment=NSTextAlignmentCenter;
        labeltext.text=[data[i] valueForKey: @"title"];
        [self.button addSubview:labeltext];
        [self.button addSubview:imagView];
       
        [cell.contentView addSubview:self.button];
    }
    
    //不能被点击
    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    
    
    
    return cell;
}


-(void)picButtonClickAction:(UIButton*)sender{
    if (sender.tag==100) {
        FunScendViewController *senderVC=[[FunScendViewController alloc]init];
        senderVC.city=self.string;
         [self setHidesBottomBarWhenPushed:YES];
        [self.navigationController pushViewController:senderVC animated:YES];
         [self setHidesBottomBarWhenPushed:NO];
    }
    if (sender.tag==101) {
        CalendarViewController*calculaVC=[[CalendarViewController alloc]init];
        //隐藏tableBar
        [self setHidesBottomBarWhenPushed:YES];
        [self.navigationController pushViewController:calculaVC animated:YES];
        [self setHidesBottomBarWhenPushed:NO];
        
    }
    if (sender.tag==102) {
        BudgetViewController *budVC=[[BudgetViewController alloc]init];
        //隐藏tabBar
         [self setHidesBottomBarWhenPushed:YES];
        [self.navigationController pushViewController:budVC animated:YES];
        //返回后还能显示tabBar
           [self setHidesBottomBarWhenPushed:NO];
    }
    if (sender.tag==132) {
        AccountViewController *accountVC=[[AccountViewController alloc]init];
        [self setHidesBottomBarWhenPushed:YES];
        [self.navigationController pushViewController:accountVC animated:YES];
        [self setHidesBottomBarWhenPushed:NO];
    }
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 220;
}

#pragma mark - 初始化
- (void) initTestData
{
   self.arr =@[@"备婚工具",@"婚礼工具"];
    NSDictionary *dic1 = @{@"title" : @"结婚登记处",
                           @"image" : @"yinzhang"};
    NSDictionary *dic2 = @{@"title" : @"结婚吉日",
                           @"image" : @"rili"};
    NSDictionary *dic3 = @{@"title" : @"结婚预算",
                           @"image" : @"jisuanqi"};
    NSDictionary *dic4 = @{@"title" : @"幸福时光",
                           @"image" : @"logo"};
    NSDictionary *dic5 = @{@"title" : @"喜帖",
                           @"image" : @"weixitie"};
    NSDictionary *dic6 = @{@"title" : @"礼金记账",
                           @"image" : @"weibiaoti"};
     self.array = [[NSMutableArray alloc] initWithObjects: @[dic1, dic2,dic3], @[dic4,dic5,dic6], nil];
}









- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
